Class CLI

Summary

Fully Qualified Name: CodeIgniter\CLI\CLI

Description

Set of static methods useful for CLI request handling.

Portions of this code were initially from the FuelPHP Framework, version 1.7.x, and used here under the MIT license they were originally made available under. Reference: http://fuelphp.com

Some of the code in this class is Windows-specific, and not possible to test using travis-ci. It has been phpunit-annotated to prevent messing up code coverage.

Some of the methods require keyboard input, and are not unit-testable as a result: input() and prompt(). validate() is internal, and not testable if prompt() isn't. The wait() method is mostly testable, as long as you don't give it an argument of "0". These have been flagged to ignore for code coverage purposes.

Methods

Name Description Defined By
beep() Beeps a certain number of times. CLI
clearScreen() Clears the screen of output CLI
color() Returns the given text with the correct color codes for a foreground and optionally a background color. CLI
error() Outputs an error to the CLI using STDERR instead of STDOUT CLI
getHeight() Attempts to determine the height of the viewable CLI window. CLI
getOption() Gets a single command-line option. Returns TRUE if the option exists, but doesn't have a value, and is simply acting as a flag. CLI
getOptionString() Returns the options as a string, suitable for passing along on the CLI to other commands. CLI
getOptions() Returns the raw array of options found. CLI
getSegment() Returns an individual segment. CLI
getSegments() Returns the raw array of segments found. CLI
getURI() Returns the command line string portions of the arguments, minus any options, as a string. This is used to pass along to the main CodeIgniter application. CLI
getWidth() Attempts to determine the width of the viewable CLI window. CLI
init() Static "constructor". CLI
input() Get input from the shell, using readline or the standard STDIN CLI
isWindows() if operating system === windows CLI
newLine() Enter a number of empty lines CLI
print() Outputs a string to the CLI without any surrounding newlines. CLI
prompt() Asks the user for input. CLI
showProgress() Displays a progress bar on the CLI. You must call it repeatedly to update it. Set $thisStep = false to erase the progress bar. CLI
strlen() Get the number of characters in string having encoded characters and ignores styles set by the color() function CLI
table() Returns a well formatted table CLI
wait() Waits a certain number of seconds, optionally showing a wait message and waiting for a key press. CLI
wrap() Takes a string and writes it to the command line, wrapping to a maximum width. If no maximum width is specified, will wrap to the window's max width. CLI
write() Outputs a string to the cli on it's own line. CLI

Method Details

beep()

Beeps a certain number of times.

Parameter Name Type Description
$num int The

Returns:

clearScreen()

Clears the screen of output

Returns: void

color()

Returns the given text with the correct color codes for a foreground and optionally a background color.

Parameter Name Type Description
$text string The
$foreground string The
$background string The
$format string Other

Returns: string The color coded string

error()

Outputs an error to the CLI using STDERR instead of STDOUT

Parameter Name Type Description
$text string|array The
$foreground string
$background string

Returns:

getHeight()

Attempts to determine the height of the viewable CLI window.

This only works on *nix-based systems, so return a sane default for Windows environments.

Parameter Name Type Description
$default int

Returns: int

getOption()

Gets a single command-line option. Returns TRUE if the option exists, but doesn't have a value, and is simply acting as a flag.

Parameter Name Type Description
$name string

Returns: bool|mixed|null

getOptionString()

Returns the options as a string, suitable for passing along on the CLI to other commands.

Returns: string

getOptions()

Returns the raw array of options found.

Returns: array

getSegment()

Returns an individual segment.

This ignores any options that might have been dispersed between valid segments in the command:

// segment(3) is 'three', not '-f' or 'anOption' > php spark one two -f anOption three

Parameter Name Type Description
$index int

Returns: mixed|null

getSegments()

Returns the raw array of segments found.

Returns: array

getURI()

Returns the command line string portions of the arguments, minus any options, as a string. This is used to pass along to the main CodeIgniter application.

Returns: string

getWidth()

Attempts to determine the width of the viewable CLI window.

This only works on *nix-based systems, so return a sane default for Windows environments.

Parameter Name Type Description
$default int

Returns: int

init()

Static "constructor".

Returns:

input()

Get input from the shell, using readline or the standard STDIN

Named options must be in the following formats: php index.php user -v --v -name=John --name=John

Parameter Name Type Description
$prefix string

Returns: string

isWindows()

if operating system === windows

Returns: bool

newLine()

Enter a number of empty lines

Parameter Name Type Description
$num int Number

Returns: void

print()

Outputs a string to the CLI without any surrounding newlines.

Useful for showing repeating elements on a single line.

Parameter Name Type Description
$text string
$foreground string|null
$background string|null

Returns:

prompt()

Asks the user for input.

Usage:

// Takes any input $color = CLI::prompt('What is your favorite color?');

// Takes any input, but offers default $color = CLI::prompt('What is your favourite color?', 'white');

// Will validate options with the in_list rule and accept only if one of the list $color = CLI::prompt('What is your favourite color?', array('red','blue'));

// Do not provide options but requires a valid email $email = CLI::prompt('What is your email?', null, 'required|valid_email');

Parameter Name Type Description
$field string Output
$options string|array String
$validation string Validation

Returns: string The user input

showProgress()

Displays a progress bar on the CLI. You must call it repeatedly to update it. Set $thisStep = false to erase the progress bar.

Parameter Name Type Description
$thisStep int|bool
$totalSteps int

Returns:

strlen()

Get the number of characters in string having encoded characters and ignores styles set by the color() function

Parameter Name Type Description
$string string

Returns: int

table()

Returns a well formatted table

Parameter Name Type Description
$tbody array List
$thead array List

Returns: void

wait()

Waits a certain number of seconds, optionally showing a wait message and waiting for a key press.

Parameter Name Type Description
$seconds int Number
$countdown bool Show

Returns:

wrap()

Takes a string and writes it to the command line, wrapping to a maximum width. If no maximum width is specified, will wrap to the window's max width.

If an int is passed into $pad_left, then all strings after the first will padded with that many spaces to the left. Useful when printing short descriptions that need to start on an existing line.

Parameter Name Type Description
$string string
$max int
$pad_left int

Returns: string

write()

Outputs a string to the cli on it's own line.

Parameter Name Type Description
$text string The
$foreground string
$background string

Returns:

Top